package com.holly.unit.deform.modular.controller;

import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.deform.api.DeformOnlineApi;
import com.holly.unit.deform.api.pojo.online.pojo.DesignFormOnlineRequest;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @Author: gxd
 * @Date: 2022-04-19
 * @Version: V1.0
 */
@Slf4j
@RestController
@ApiResource(name = "online表单开发")
@Api(tags = "online表单开发")
public class DeformOnlineController {


    @Resource
    private DeformOnlineApi api;

    /**
     * 分页列表查询（主表）
     * @param designFormOnlineDTO
     * @param req
     * @return
     */
    @ApiOperation(value = "分页列表查询" ,notes = "分页列表查询")
    @GetMapping(value = "/online/queryPageList")
    public ResponseData queryPageList(DesignFormOnlineRequest designFormOnlineDTO, HttpServletRequest req) {
        String username = LoginContext.me().getLoginUser().getAccount();



        return new SuccessResponseData(api.queryPageList(designFormOnlineDTO));
    }


    /**
     * 新增表单
     * @param designFormOnlineDTO
     * @return
     */
    @ApiOperation(value = "新增表单" ,notes = "新增表单")
    @PostMapping(value = "/online/add")
    public ResponseData add(@RequestBody @Validated(DesignFormOnlineRequest.add.class) DesignFormOnlineRequest designFormOnlineDTO) {
        api.add(designFormOnlineDTO);
        return new SuccessResponseData() ;
    }

    /**
     * 查询表字段
     */
    @GetMapping(value = "/online/listField")
    public SuccessResponseData listField(@RequestParam(name = "id", required = true) String id) {
        return new SuccessResponseData(api.listByHead(id));
    }

    /**
     * 新增表单
     * @param designFormOnlineDTO
     * @return
     */
    @ApiOperation(value = "修改表单" ,notes = "修改表单")
    @PostMapping(value = "/online/update")
    public ResponseData update(@RequestBody @Validated(DesignFormOnlineRequest.edit.class)  DesignFormOnlineRequest designFormOnlineDTO) {
        api.update(designFormOnlineDTO);
        return new SuccessResponseData() ;
    }

    /**
     * 查询数据库所有表
     * @param req
     * @return
     */
    @ApiOperation(value = "查询数据库所有表" ,notes = "查询数据库所有表")
    @GetResource(name = "查询数据库所有表", path = "/onlineCgFrom/queryPageListFrom",requiredPermission = false, requiredLogin = false)
    public ResponseData queryPageList(HttpServletRequest req) {
        return new SuccessResponseData(api.queryPageListFrom());
    }


    /**
     * 查询数据库表字段
     * @return
     */
    @ApiOperation(value = "查询数据库表字段" ,notes = "查询数据库表字段")
    @GetResource(name = "查询数据库表字段", path = "/onlineCgFrom/listByHeadId",requiredPermission = false, requiredLogin = false)
    public ResponseData listByHeadId(@RequestParam(value="cgformCode") String cgformCode) {
        return new SuccessResponseData(api.listByHeadId(cgformCode));
    }


    /**
     * 删除自定义表
     */
    @GetMapping(value = "/online/deleteTable")
    public SuccessResponseData deleteTable(@RequestParam(name = "id") String id,@RequestParam(name = "tableName") String tableName) {
        api.deleteTable(id,tableName);
        return new SuccessResponseData();
    }

}
